package org.example.weblog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.example.weblog.entity.User;
import org.example.weblog.jwt.JwtTokenUtil;
import org.example.weblog.mapper.UserMapper;
import org.example.weblog.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;

import java.util.List;

public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    private UserMapper userMapper;

    private JwtTokenUtil jwtTokenUtil;

    private static final Logger log = LoggerFactory.getLogger(UserServiceImpl.class);

    // 用户名正则表达式：5到10位数字或符号混合
    private static final String USERNAME_PATTERN = "^(?=.*\\d)(?=.*[\\p{Punct}]).{5,10}$";

    // 密码正则表达式：6到12位数字或字母混合
    private static final String PASSWORD_PATTERN = "^(?=.*\\d)(?=.*[a-zA-Z]).{6,12}$";

    public String registerUser(String username, String password) {
        if (!StringUtils.hasText(username) || !StringUtils.hasText(password)) {
            throw new IllegalArgumentException("用户名和密码不能为空");
        }

        if (!username.matches(USERNAME_PATTERN)) {
            throw new IllegalArgumentException("用户名必须是5到10位，且包含至少一个数字和一个符号");
        }

        if (!password.matches(PASSWORD_PATTERN)) {
            throw new IllegalArgumentException("密码必须是6到12位，且包含至少一个数字和一个字母");
        }
        // 检查用户名是否已存在
        if (getByName(username) != null) {
            throw new IllegalArgumentException("用户名已存在");
        }

        // 创建新用户
        User user = new User();
        user.setUsername(username);
        user.setPassword(password); // 注意：在实际应用中，密码应该进行加密存储
        addUser(user);

        // 生成并返回 token
        String token = jwtTokenUtil.generateToken(username);
        log.info("用户注册成功，生成的 token: {}", token);
        return token;
    }

    // 根据用户名和密码获取用户
    @Override
    public User getByNameAndPassword(String name, String password) {
        log.info("根据用户名和密码获取用户: {} 和 {}", name, password);
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getUsername, name)
                .eq(User::getPassword, password);
        return userMapper.selectOne(wrapper);
    }

    // 根据用户名获取用户
    @Override
    public User getByName(String name) {
        log.info("根据用户名获取用户: {}", name);
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getUsername, name);
        return userMapper.selectOne(wrapper);
    }

    // 添加用户
    @Override
    public void addUser(User user) {
        log.info("添加用户: {}", user);
        userMapper.insert(user);
    }

    // 获取所有用户
    @Override
    public List<User> getAllUsers() {
        log.info("获取所有用户");
        try {
            return userMapper.selectList(null);
        } catch (Exception e) {
            log.error("获取所有用户时出错: ", e);
            throw e; // 重新抛出异常，让全局异常处理器处理
        }
    }
}